package com.ksd.modules.download.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.download.entity.AdminDownloadsCode;
import com.ksd.modules.download.mapper.AdminDownloadsCodeMapper;
import com.ksd.modules.download.po.AdminDownloadsCodeSavePO;
import com.ksd.modules.download.po.AdminDownloadsCodeSearchPO;
import com.ksd.modules.download.service.IAdminDownloadsCodeService;
import com.ksd.modules.download.vo.AdminDownloadsCodeVo;
import com.ksd.utils.snow.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminDownloadsCodeServiceImpl extends BaseServiceImpl<AdminDownloadsCodeMapper, AdminDownloadsCode> implements IAdminDownloadsCodeService {

    /**
     * 查询列表
     * @param adminDownloadsCodeSearchPO
     * @return
     */
    public Page<AdminDownloadsCodeVo> findAdminDownloadsCodes(AdminDownloadsCodeSearchPO adminDownloadsCodeSearchPO){
        Page<AdminDownloadsCode> page = new Page<>(adminDownloadsCodeSearchPO.getPageNo(),adminDownloadsCodeSearchPO.getPageSize());
        LambdaQueryWrapper<AdminDownloadsCode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminDownloadsCode::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsCodeSearchPO.getOpid()),AdminDownloadsCode::getOpid,adminDownloadsCodeSearchPO.getOpid());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsCodeSearchPO.getMark()),AdminDownloadsCode::getMark,adminDownloadsCodeSearchPO.getMark());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminDownloadsCodeSearchPO.getCode()), AdminDownloadsCode::getCode,adminDownloadsCodeSearchPO.getCode());
        lambdaQueryWrapper.orderByDesc(AdminDownloadsCode::getCreateTime);
        Page<AdminDownloadsCode> adminDownloadsPage = this.page(page, lambdaQueryWrapper);
        Page<AdminDownloadsCodeVo> adminDownloadsVoPage = tranferPageBo(adminDownloadsPage, AdminDownloadsCodeVo.class);
        return adminDownloadsVoPage;
    }



    /**
     * 创建兑换码
     * @param adminDownloadsCodeSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean createBatchCode(AdminDownloadsCodeSavePO adminDownloadsCodeSavePO){
        AdminDownloadsCode adminUserWalletCode= tranferBo(adminDownloadsCodeSavePO, AdminDownloadsCode.class);
        adminUserWalletCode.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminUserWalletCode.setSystemName(SystemIdThrealLocal.get().getSystemName());
        adminUserWalletCode.setCode(new SnowflakeIdWorker(9,8).nextId()+"");
        adminUserWalletCode.setMark(0);
        this.saveOrUpdate(adminUserWalletCode);
        return true;
    }


    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminDownloadsCodeById(Long id){
        boolean b = this.removeById(id);
        return b;
    }


    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminDownloadsCode(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        return b;
    }


}
